home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / MISC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-18  |  6.7 KB  |  341 lines

  1. /* Miscellaneous machine independent utilities
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "commands.h"
  6. #include "socket.h"
  7.  
  8. #if !defined(_lint)
  9. static char rcsid[] OPTIONAL = "$Id: misc.c,v 1.22 1997/08/19 01:19:22 root Exp root $";
  10. #endif
  11.  
  12. static int htob (char c);
  13. const char *tcp_port_name (int portnum);
  14.  
  15. extern int Mprunning;
  16.  
  17. char Whitespace[] = " \t\r\n";
  18.  
  19. struct port_table {
  20.     const char *name;
  21.     int port;
  22. };
  23.  
  24. static struct port_table tcp_port_table[] =
  25. {
  26.     { "*",        0 },
  27.     { "bootpcli",    IPPORT_BOOTPC },
  28.     { "bootpsvr",    IPPORT_BOOTPS },
  29.     { "callbook",    IPPORT_CALLDB },
  30.     { "cbs",    IPPORT_CBS },
  31.     { "convers",    IPPORT_CONVERS },
  32.     { "daytime",    IPPORT_DAYTIME },
  33.     { "discard",    IPPORT_DISCARD },    /* ARPA discard protocol */
  34.     { "domain",    IPPORT_DOMAIN },    /* ARPA domain nameserver */
  35.     { "echo",    IPPORT_ECHO },        /* ARPA echo protocol */
  36.     { "finger",    IPPORT_FINGER },    /* ARPA finger protocol */
  37.     { "fbbtelnet",    IPPORT_FTELNET },
  38.     { "ftp",    IPPORT_FTP },        /* ARPA file transfer protocol (cmd) */
  39.     { "ftp-data",    IPPORT_FTPD },        /* ARPA file transfer protocol (data) */
  40.     { "http",    IPPORT_HTTP },
  41.     { "info",    IPPORT_INFO },
  42.     { "netupds",    4715 },
  43.     { "news",    IPPORT_NEWS },
  44.     { "nntp",    IPPORT_NNTP },
  45.     { "pop2",    IPPORT_POP2 },        /* Post Office Prot. v2 */
  46.     { "pop3",    IPPORT_POP3 },        /* Post Office Prot. v3 */
  47.     { "quote",    IPPORT_QUOTE },
  48.     { "remote",    IPPORT_REMOTE },
  49.     { "rip",    IPPORT_RIP },
  50.     { "rlogin",    IPPORT_RLOGIN },
  51.     { "rwho",    IPPORT_RWHO },
  52.     { "smtp",    IPPORT_SMTP },        /* ARPA simple mail transfer protocol */
  53.     { "telnet",    IPPORT_TELNET },    /* ARPA virtual terminal protocol */
  54.     { "term",    IPPORT_TERM },        /* Serial interface server port */
  55.     { "tftpd",    IPPORT_TFTPD },
  56.     { "time",    IPPORT_TIME },
  57.     { "timed",    IPPORT_TIMED },
  58.     { "trace",    IPPORT_TRACE },
  59.     { "ttylink",    IPPORT_TTYLINK },
  60.     { "tutor",    IPPORT_TUTOR },
  61.     { "xconvers",    IPPORT_XCONVERS },
  62.     { "xwindows",    IPPORT_X },
  63.     { NULLCHAR,    0 }
  64. };
  65.  
  66.  
  67.  
  68. /* convert a tcp port number or name to integer - WG7J */
  69. int
  70. atoip (s)
  71. char *s;
  72. {
  73. struct port_table *port = tcp_port_table;
  74. int p;
  75. size_t n;
  76.  
  77.     if (!s)
  78.         return 0;
  79.     if ((p = atoi (s)) == 0) {
  80.         n = strlen (s);
  81.         for (; port->name; port++) {
  82.             if (!strncmp (s, port->name, n)) {
  83.                 p = port->port;
  84.                 break;
  85.             }
  86.         }
  87.     }
  88.     return p;
  89. }
  90.  
  91.  
  92.  
  93. const char *
  94. tcp_port_name (int portnum)
  95. {
  96. struct port_table *port = tcp_port_table;
  97. static char buf[11];
  98.  
  99.     for (; port->name; port++) {
  100.         if (port->port == portnum)
  101.             return (port->name);
  102.     }
  103.     sprintf (buf, "%u", portnum);
  104.     return (buf);
  105. }
  106.  
  107.  
  108.  
  109. /* Select from an array of strings, or return ascii number if out of range */
  110. const char *
  111. smsg (msgs, nmsgs, n)
  112. const char *msgs[];
  113. unsigned nmsgs, n;
  114. {
  115. static char buf[16];
  116.  
  117.     if (n < nmsgs && msgs[n] != NULLCHAR)
  118.         return msgs[n];
  119.     sprintf (buf, "%u", n);
  120.     return buf;
  121. }
  122.  
  123.  
  124.  
  125. /* Convert hex-ascii to integer */
  126. int
  127. htoi (s)
  128. const char *s;
  129. {
  130. int i = 0;
  131. char c;
  132.  
  133.     while ((c = *s++) != '\0') {
  134.         if (c == 'x')
  135.             continue;    /* allow 0x notation */
  136.         if ('0' <= c && c <= '9')
  137.             i = (i * 16) + (c - '0');
  138.         else if ('a' <= c && c <= 'f')
  139.             i = (i * 16) + (c - 'a' + 10);
  140.         else if ('A' <= c && c <= 'F')
  141.             i = (i * 16) + (c - 'A' + 10);
  142.         else
  143.             break;
  144.     }
  145.     return i;
  146. }
  147.  
  148.  
  149.  
  150. /* Convert single hex-ascii character to binary */
  151. static int
  152. htob (c)
  153. char c;
  154. {
  155.     if ('0' <= c && c <= '9')
  156.         return c - '0';
  157.     else if ('a' <= c && c <= 'f')
  158.         return c - 'a' + 10;
  159.     else if ('A' <= c && c <= 'F')
  160.         return c - 'A' + 10;
  161.     else
  162.         return -1;
  163. }
  164.  
  165.  
  166.  
  167. /* Read an ascii-encoded hex string, convert to binary and store in
  168.  * output buffer. Return number of bytes converted
  169.  */
  170. int
  171. readhex (out, in, size)
  172. char *out, *in;
  173. int size;
  174. {
  175. int c, count;
  176.  
  177.     if (in == NULLCHAR)
  178.         return 0;
  179.     for (count = 0; count < size; count++) {
  180.         while (*in == ' ' || *in == '\t')
  181.             in++;            /* Skip white space */
  182.         if ((c = htob (*in++)) == -1)
  183.             break;            /* Hit non-hex character */
  184.         out[count] = (char) (c << 4);    /*lint !e701 * First nybble */
  185.         while (*in == ' ' || *in == '\t')
  186.             in++;            /* Skip white space */
  187.         if ((c = htob (*in++)) == -1)
  188.             break;            /* Hit non-hex character */
  189.         out[count] |= (char) c;        /* Second nybble */
  190.     }
  191.     return count;
  192. }
  193.  
  194.  
  195.  
  196. /* replace terminating end of line marker(s) with null */
  197. void
  198. rip (s)
  199. register char *s;
  200. {
  201. register char *cp;
  202.  
  203. #ifndef TNOS_68K
  204.     while ((cp = strchr (s, '\n')) != NULLCHAR || (cp = strchr (s, '\r')) != NULLCHAR)
  205. #else
  206.     while ((cp = strchr (s, '\l')) != NULLCHAR || (cp = strchr (s, '\r')) != NULLCHAR)
  207. #endif
  208.         *cp = '\0';
  209. }
  210.  
  211.  
  212.  
  213. /* Copy a string to a malloc'ed buffer. Turbo C has this one in its
  214.  * library, but it doesn't call mallocw() and can therefore return NULL.
  215.  * NOS uses of strdup() generally don't check for NULL, so they need this one.
  216.  */
  217. char *
  218. strdup (s)
  219. const char *s;
  220. {
  221. register char *out;
  222. register int len;
  223.  
  224.     if (s == NULLCHAR)
  225.         return NULLCHAR;
  226.     len = (int) strlen (s);
  227.     out = mallocw ((unsigned) len + 1);
  228.     /* This is probably a tad faster than strcpy, since we know the len */
  229.     memcpy (out, s, (size_t) len);
  230.     out[len] = '\0';
  231.     return out;
  232. }
  233.  
  234.  
  235.  
  236. /* Host-network conversion routines, replaced on the x86 with
  237.  * assembler code in pcgen.asm
  238.  */
  239. /* Put a long in host order into a char array in network order */
  240. unsigned char *
  241. put32 (cp, x)
  242. register unsigned char *cp;
  243. uint32 x;
  244. {
  245.     *cp++ = uchar((x >> 24) & 0xff);
  246.     *cp++ = uchar((x >> 16) & 0xff);
  247.     *cp++ = uchar((x >> 8) & 0xff);
  248.     *cp++ = uchar(x & 0xff);
  249.     return cp;
  250. }
  251.  
  252.  
  253.  
  254. /* Put a short in host order into a char array in network order */
  255. unsigned char *
  256. put16 (cp, x)
  257. register unsigned char *cp;
  258. int16 x;
  259. {
  260.     *cp++ = uchar((x >> 8) & 0xff);
  261.     *cp++ = uchar(x & 0xff);
  262.     return cp;
  263. }
  264.  
  265.  
  266.  
  267. int16
  268. get16 (cp)
  269. register char *cp;
  270. {
  271. register int16 x;
  272.  
  273.     x = uchar (*cp++);
  274.     x <<= 8;
  275.     x |= uchar (*cp);
  276.     return x;
  277. }
  278.  
  279.  
  280.  
  281. /* Machine-independent, alignment insensitive network-to-host long conversion */
  282. uint32
  283. get32 (cp)
  284. register char *cp;
  285. {
  286. uint32 rval;
  287.  
  288.     rval = uchar (*cp++);
  289.     rval <<= 8;
  290.     rval |= uchar (*cp++);
  291.     rval <<= 8;
  292.     rval |= uchar (*cp++);
  293.     rval <<= 8;
  294.     rval |= uchar (*cp);
  295.  
  296.     return rval;
  297. }
  298.  
  299.  
  300.  
  301. #ifdef MSDOS        /* used by alloc.c */
  302. /* Compute int(log2(x)) */
  303. int
  304. log2 (x)
  305. register int16 x;
  306. {
  307. register int n = 16;
  308.  
  309.     for (; n != 0; n--) {
  310.         if (x & 0x8000)
  311.             break;
  312.         x <<= 1;
  313.     }
  314.     n--;
  315.     return n;
  316. }
  317. #endif
  318.  
  319.  
  320.  
  321. void
  322. kmutex_lock (int *key)
  323. {
  324.     while (Mprunning == 1 && *key == TNOS_MUTEX_LOCKED)
  325.         kwait (key);
  326.     *key = TNOS_MUTEX_LOCKED;
  327. }
  328.  
  329. void
  330. kmutex_unlock (int *key)
  331. {
  332. int errnosave = errno;
  333.  
  334.     *key = TNOS_MUTEX_UNLOCKED;
  335.     if (Mprunning == 1)    {
  336.         ksignal (key, 1);
  337.         kwait (NULL);
  338.     }
  339.     errno = errnosave;
  340. }
  341.